package com.financing.platform.task;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.financing.platform.dto.FpNewProductDTO;
import com.financing.platform.util.HtmlUnitFetchUtil;
import com.financing.platform.util.YicaifuWbClient;
import com.financing.platform.web.api.YCFProductInfoAction;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

/**
 * 
 * @ClassName: yCFProductInfoJob
 * @Description:壹财富产品
 *
 */
@Component("yCFProductInfoJob")
public class YCFProductInfoJob {
	
	private Logger log = Logger.getLogger(this.getClass());
	
	@Autowired
	YCFProductInfoAction ycfProductInfoAction;
//	@Scheduled(cron = "00 30 10 * * ?") 
	public void YCFProductInfoJob() throws Exception{
		try {
			HtmlPage html = HtmlUnitFetchUtil.getHtml("http://1212.ip138.com/ic.asp");
			String ip = html.asXml();
			if(ip.contains("203.156.233.216")||ip.contains("116.226.248.105")){
				log.info("测试环境不执行  抓取壹财富  脚本");
				return ;
			}
			ycfProductInfoAction.YCFProductInfoJob(null, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
//	@Scheduled(cron = "00 30 09 * * ?")  
	public void YCFProductInfoJobB() throws Exception{
		YCFProductInfoJob();
	}
	
	public static void main(String[] args) throws Exception {
		//壹财富登录   账号  随机切换
		YicaifuWbClient.LoginYCF("18016325069","2af9b1ba42dc5eb01743e6b3759b6e4b");
		//赋值cookie 
		YicaifuWbClient.httpToWebClient();
		//壹财富数据      预售和在售
//		String ycfTrustUrl="http://1caifu.com/trust/1/?issaleses=10,20";
		String ycfTrustUrl="http://1caifu.com/trust/1/?id=1&productTypeId=1&issaleses=10,20&sortBy=0&ag=1&_="+System.currentTimeMillis();
		//壹财富数据      预售和在售
		String ycfassetManagementUrl="http://1caifu.com/asset-management/1/?id=1&productTypeId=2&issaleses=10,20&sortBy=0&ag=1&_="+System.currentTimeMillis();
		
		//壹财富数据  债权  预售和在售
//		String ycfobligationfundUrl="http://1caifu.com/obligation-fund";
		
		String urlDetailRegex="/.+_.+\\.html";
		//获取到所有的详情页地址
		List<HtmlAnchor> trustList=YicaifuWbClient.getListUrl(ycfTrustUrl,urlDetailRegex);
		List<HtmlAnchor> assetManagementList=YicaifuWbClient.getListUrl(ycfassetManagementUrl,urlDetailRegex);
		System.out.println(trustList.size());
		
		for (HtmlAnchor htmlAnchor : assetManagementList) {
			HtmlPage click=YicaifuWbClient.click(htmlAnchor);
			//获取产品的信息   信托
			FpNewProductDTO initInfo = YicaifuWbClient.initInfo20160612(click);
			System.out.println(initInfo.toString());
			break;
		}
		
		System.exit(0);
		File xintuo=new File("D://xintuo.txt");
		File ziguan=new File("D://ziguan.txt");
		File simu=new File("D://simu.txt");

		int min =1;
		int max=143;
		String type="xintuo";
		List<String> xtPName=getProductName(min, max, type);
		IOUtils.writeLines(xtPName,  "\n", new FileOutputStream(xintuo));
		
		int zgmin =1;
		int zgmax=49;
		String zgtype="ziguan";
		List<String> zgPName=getProductName(zgmin, zgmax, zgtype);
		IOUtils.writeLines(zgPName, "\n", new FileOutputStream(ziguan));
		
		int smmin =1;
		int smmax=18;
		String smtype="simu";
		List<String> smPName=getProductName(smmin, smmax, smtype);
		IOUtils.writeLines(smPName, "\n", new FileOutputStream(simu));
		
	}

	public static List<String> getProductName(int min, int max, String type) {
		List<String> list=new ArrayList<String>();
		Set<String> set=new TreeSet<String>();
		for (int i = min; i < max; i++) {
			System.out.println("第："+i+"页");
			//有单产品名称  导出excel
			String xintuo="http://www.youdan.me/"+type+"_%s_%s.html";
			xintuo=String.format(xintuo, i,max);
			HtmlPage html = HtmlUnitFetchUtil.getHtml(xintuo);
			String fecthAll = HtmlUnitFetchUtil.fecthAll(html.asXml(),"<a href=\"/"+type+"_[0-9a-zA-Z]+\\.html\" title=\"(.*?)\"",1);
			System.out.println(fecthAll);
			String[] split = fecthAll.split(";");
			for (String s : split) {
				if(!set.contains(s))
					list.add(s);
				set.add(s);
			}
		}
		return list;
	}
}
